﻿use QLRAPPHIM
go
--------------------
create Procedure sp_LostUpdate_error
@MaVe varchar(10),
@LoaiVe bit,
@output varchar(100) output
as
begin
 begin tran lostupdate_error

declare @tinhtrang int
select @tinhtrang = TinhTrangVe from Ve 
where MaVe = @MaVe

waitfor  DELAY '00:00:10'
if @tinhtrang = 0 
begin
update Ve set TinhTrangVe = 1,NguoiLon_TreEm = @LoaiVe where MaVe = @MaVe
 set @output =   'Ban da dat ve thanh cong!'
end
else
set @output = 'Dat ve khong thanh cong. Vi ve da co nguoi dat roi!'

commit tran lostupdate_error
end


go
-------------------------------------------------------------

create Procedure sp_LostUpdate_fix
@MaVe varchar(10),
@LoaiVe bit,
@output varchar(100) output
as
begin
 begin tran lostupdate_fix
set  transaction isolation level  repeatable read 
declare @tinhtrang int
select @tinhtrang = TinhTrangVe from Ve with(UPDLOCK)
where MaVe = @MaVe

waitfor  DELAY '00:00:10'
if @tinhtrang = 0 
begin

update Ve set TinhTrangVe = 1,NguoiLon_TreEm = @LoaiVe where MaVe = @MaVe
 set @output =  'Ban da dat ve thanh cong!'
end
else
set @output = 'Dat ve khong thanh cong. Vi ve da co nguoi dat roi!'
commit tran lostupdate_fix
end
-------------------------------
create Procedure sp_LostUpdateDeadlock_erorr
@MaVe varchar(10),
@LoaiVe bit,
@output varchar(100) output
as
begin
 begin tran lostupdate_error
set  transaction isolation level  serializable 
declare @tinhtrang int
select @tinhtrang = TinhTrangVe from Ve
where MaVe = @MaVe

waitfor  DELAY '00:00:10'
if @tinhtrang = 0 
begin

update Ve set TinhTrangVe = 1,NguoiLon_TreEm = @LoaiVe where MaVe = @MaVe
 set @output =  'Ban da dat ve thanh cong!'
end
else
set @output = 'Dat ve khong thanh cong. Vi ve da co nguoi dat roi!'
commit tran lostupdate_error
end
